package com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity;

import android.app.Activity;
import android.content.Context;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;

import android.widget.LinearLayout;


import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;


import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/1.
 * 商品详情
 */

public class CommodityDetailsActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.ad_pager)//轮播
            ViewPager adPager;
    @Bind(R.id.ad_layer)//圆圈
            LinearLayout adLayer;
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };

    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_details);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);

        List<String> getinforresult = new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(), getinforresult);

    }
    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;

        }
    }
}

